//Function to validate the form,fieldRequired,fieldDescription
function validate(formobj,fieldRequired,fieldDescription,email)
{
	
	var alertMsg = "Following fields are missing or incorrect :\n";
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++)
	{
		var obj     = formobj.elements[fieldRequired[i]];
		if (obj)
		{
			switch(obj.type)
			{
				case "select-one":
				if(obj.options.length > 1)
				{
					if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == ""){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
				}
				break;
				
				case "select-multiple":
				if (obj.selectedIndex == -1)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
				
				case "text":
				case "password":
				case "textarea":
				if (trim_string(obj.value) == "" || trim_string(obj.value) == null)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				
				if(obj.id == 'conf_password' && trim_string(obj.value) != "")
				{
					tempMsg = confirm_password(formobj);
					if(tempMsg != '')
						alertMsg += tempMsg; 
				}
				break;
				
				case "file":
				if (trim_string(obj.value) == "" || trim_string(obj.value) == null)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				else
				{
					msg = extension_check(obj.value);
					if(msg != '')
					alertMsg += msg;
				}
				break;
				default:
			}
			if (obj.type == undefined)
			{
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++)
				{
					if (obj[j].checked)
					{
						blnchecked = true;
					}
				}
				if (!blnchecked)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
		
		// check valid email
		if( email == 1 && ( obj.name == 'userEmail' || obj.name == 'email' ) )
		{
			if( obj.value != '' )
			{
				if( !verify_email( obj.value ) )
					alertMsg += ' - Enter valid E-Mail address'; 
			}
		}
	}
	
	if (alertMsg.length == l_Msg)
	{
		return true;
	}
	else
	{
		alert(alertMsg);
		return false;
	}
}




// show customer detail ( send request )
function show_detail( idval, userId )
{   
	var url = "ajax_call.php?id="+idval+"&action=customer_info&userId="+userId; 
	
	xmlHttp = GetXmlHttpObject( show_detail_response );
	xmlHttp.open( "GET", url , true );
	xmlHttp.send( null );
}


// show customer detail ( handle response )
function show_detail_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
	{
		var arr = xmlHttp.responseText.split( '|' );
		document.getElementById( 'user_info' ).innerHTML = arr[0];
		document.getElementById( 'past_order' ).innerHTML = arr[1];
	}
}

function confirm_password(formobj)
{
	var msg ='';
	var password = formobj.elements['password'];
	var conPass  = formobj.elements['conf_password'];
	if(trim_string(password.value) != trim_string(conPass.value))
	{
		msg = ' - Confirm password \n';
	}
	return msg;
}

function trim_string (str) 
{
  while (str.charAt(0) == ' ')
    str = str.substring(1);
  while (str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length - 1);
  return str;
}

function verify_email(str){
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str))){
		return false;
	}
	else{
		return true;
	}
}

function drop(){
	if(confirm('Are you sure to delete?'))
		return true;
	else
		return false;
}

function status(){
	if(confirm('Are you sure to change status of following records?'))
		return true;
	else
		return false;
}

function change_status(obj){
	chkFlag = 0;
	if(obj){
		for(i=0; i< obj.elements.length; i++){
			if(obj.elements[i].type=='checkbox'){
				if(obj.elements[i].checked == true){
					chkFlag = 1;
					break;
				}
			}
		}
	}
	else{
		alert('Error in script');
		return false;
	}
	if(chkFlag == 1){
		return status();
	}
	else{
		alert('Please select at least one record to change status!');
		return false;
	}
}

function drop_validate(obj){
	chkFlag = 0;
	if(obj){
		for(i=0; i< obj.elements.length; i++){
			if(obj.elements[i].type=='checkbox'){
				if(obj.elements[i].checked == true){
					chkFlag = 1;
					break;
				}
			}
		}
	}
	else{
		alert('Error in script');
		return false;
	}
	if(chkFlag == 1){
		return drop();
	}
	else{
		alert('Please select at least one record to delete!');
		return false;
	}
}

//Function to check or uncheck all the check box in a form.
function set_all_chk(obj){
	var set = document.getElementById('setAll');
	if(set.checked == true){
		for(i=0;i<obj.elements.length;i++){
			obj[i].checked = true;
		}
	}
	else{
		for(i=0;i<obj.elements.length;i++){
			obj[i].checked = false;
		}
	}
 }
 
// select all elements of a form
function checkAll( form )
{
	var frm = document.getElementById( form );
	for( i = 0; i < frm.elements.length; i++ )
	{
		if ( frm.elements[i].type == "checkbox" )
			frm.elements[i].checked = true;
	}
}

// uncheck ALL elemnts of a form
function uncheckAll( form )
{
	var frm = document.getElementById( form );
	for( i = 0; i < frm.elements.length; i++ )
	{
		if ( frm.elements[i].type == "checkbox" )
			frm.elements[i].checked = false;
	}
}
 

// delete and validate
function del_n_validate( url, del_id )
{
	if(confirm('Are you sure to delete?'))
	{
		window.location.href=url+'?del_id='+del_id;
		return true;
	}
	else
		return false;
}


function popup(url,title,w,h,r,s,m,t){
	//if vars aren't provided, use these defaults
	if (title==null) {
		title = 'popup';
	} if (w==null) {
		w=600;
	} if (h==null) {
		h=570;
	} if (r==null) {
		r='yes';
	} if (s==null) {
		s='yes';
	} if (m==null) {
		m='no';
	} if (t==null) {
		t='no';
	}
	vars = 'width='+w+',height='+h+',resizable='+r+',scrollbars='+s+',menubar='+m+',toolbar='+t;
	var newWin = window.open(url,title,vars);
	newWin.focus();
	return false;
}


// Function to get options list (send request)
function get_option_list( idval )
{   
	
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url = "edit_records_ajax.php?pId="+idval+"&sec="+seconds; 
	xmlHttp = GetXmlHttpObject( option_list_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to get states ( handle response )
function option_list_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		document.getElementById( "optionListTxt" ).innerHTML = xmlHttp.responseText;
	else if( xmlHttp.readyState != 4 )
		document.getElementById( "optionListTxt" ).innerHTML = 'Loading ......';
} 


// Function to Edit record of group options (send request)
function edit_group_options( frm , groupId , productId )
{   
	// declare variables
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url;
	var val = '';
	var status = document.getElementById('optionStatus').value;
	var price = document.getElementById('productPrice').value;
	// fetch all options
	for ( var i = 0; i < frm.elements.length; i++ )
		if ( frm.elements[i].type == "checkbox" )
			if ( frm.elements[i].checked == true )
				val += frm.elements[i].value + '|';
	
	// remove last pipe character
	val = val.substring( 0, val.length - 1 );
	
	// send request
	url = "edit_records_ajax.php?groupId="+groupId+"&sec="+seconds+"&productId="+productId+"&value="+val+"&price="+price+"&status="+status; ; 
	xmlHttp = GetXmlHttpObject( edit_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}


// Function to Edit record (send request)
function edit_record( idval )
{   
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var val	=	document.getElementById('Txt'+idval).value;
	var url = "edit_records_ajax.php?recId="+idval+"&sec="+seconds+"&value="+val; 
	xmlHttp = GetXmlHttpObject( edit_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to edit record ( handle response )
function edit_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		alert(xmlHttp.responseText);
} 



// Function to get states (send request)
function get_states( idval )
{   
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url = "get_states.php?CatID="+idval+"&sec="+seconds; 
	xmlHttp = GetXmlHttpObject( state_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to get states ( handle response )
function state_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		document.getElementById( "stateTxt" ).innerHTML = xmlHttp.responseText;
	else if( xmlHttp.readyState != 4 )
		document.getElementById( "stateTxt" ).innerHTML = 'Loading ......';
} 


// Function to set default autoresponders
function autoresponder( userId, responderId )
{   
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url = "default_autoresponder.php?userId="+userId+"&responderId="+responderId+"&sec="+seconds; 
	xmlHttp = GetXmlHttpObject( responder_response );
	xmlHttp.open("GET", url , true );
	xmlHttp.send( null );
}

// Function to autoresponder ( handle response )
function responder_response()
{ 
	if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		alert( xmlHttp.responseText );
	else if( xmlHttp.readyState != 4 )
		;
} 


// Function for initialilize GetXmlHttpObject
function GetXmlHttpObject( handler )
{ 
	var objXmlHttp=null
	
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This example doesn't work in Opera") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5") >= 0 )
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}



// Function for show helloworld tag
function showHello()
{
	if ( document.getElementById('hello_worldTr').style.display == 'none' )
		document.getElementById('hello_worldTr').style.display = 'inline';
	else
		document.getElementById('hello_worldTr').style.display = 'none';
}





