var newWin = null; 
function popUp(strURL, strType, strHeight, strWidth) { 
 var strOptions=""; 
 if (strType=="console") 
   strOptions="resizable,height="+ 
     strHeight+",width="+strWidth; 
 if (strType=="fixed") 
   strOptions="status,height="+ 
     strHeight+",width="+strWidth; 
 if (strType=="fixedscroll") 
   strOptions="status,scrollbars,height="+ 
     strHeight+",width="+strWidth; 
if (strType=="fixedpos") 
   strOptions="status,height,screenX=300,left=300,screenY=300,top=300,height="+ 
     strHeight+",width="+strWidth; 
 if (strType=="elastic") 
   strOptions="scrollbars,"+ 
     "resizable,height="+
     strHeight+",width="+strWidth; 
 if (strType=="elasticmenu") 
   strOptions="menubar,scrollbars,"+ 
     "resizable,height="+ 
     strHeight+",width="+strWidth; 
 newWin = window.open(strURL, 'newWin', strOptions); 
 newWin.focus();
 
}

function field_hash(temp) {

	var fields = new Array();
	fields['form_field_name'] 		= 'Name';
	fields['form_field_address1']	= 'Address field 1';
	fields['form_field_address2']	= 'Address field 2';
	fields['form_field_town']		= 'Town';
	fields['form_field_postcode']	= 'Postcode';
	fields['form_field_country']	= 'Country';
	fields['form_field_email']		= 'Email';
	fields['form_field_mobile']		= 'Mobile';

	var i = 0;	
	for (i in fields) { if (i == temp) break }
	return fields[i];
	
} // end of field_hash(temp)

function todelete() {

		check = confirm('Are you sure you want to delete your account?');
		if ( check == true) {
			document.deleteform.submit();
		}

}

function isblank(s){
	for(var i=0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
} // end of isblank()


function CheckEmail(emailStr){
	var emailPat = /(^[\w-_àÀáÁâÂãÃäÄåÅæÆçÇèÈéÉêÊëËìÌíÍîÎïÏðÐñÑòÒóÓôÔõÕöÖùÙúÚûÛüÜýÝÿß\.]*[\w-_àÀáÁâÂãÃäÄåÅæÆçÇèÈéÉêÊëËìÌíÍîÎïÏðÐñÑòÒóÓôÔõÕöÖùÙúÚûÛüÜýÝÿß\.])@(\[\d{1,3}(\.\d{1,3}){3}]|[\w-_\.]*[\w-_\.]+)$/;
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) return false;
	// make sure the IP address domain is valid
	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null){
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255)
				return false;
			}
 		}
	return true;
}


function CheckName(n){
	var valid = 1;
	var GoodChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-àÀáÁâÂãÃäÄåÅæÆçÇèÈéÉêÊëËìÌíÍîÎïÏðÐñÑòÒóÓôÔõÕöÖùÙúÚûÛüÜýÝÿß /()';
	for(var i =0; i <= n.length -1; i++) {
		if (GoodChars.indexOf(n.charAt(i)) == -1) 
                   valid = 0;
        }
        return valid;
}


function CheckPhoneNumber(c) {
	var GoodChars = '0123456789';
	if (c.length == 0)	return true;
	if (c.length < 12) 	return false;
	for(var i=0; i <= c.length -1; i++) {
		if (GoodChars.indexOf(c.charAt(i)) == -1)
			return false;
	} 
	return true;
}


function verify(f){
	var msg= 'Your details were not submitted due to the following reasons \n______________________________________________________\n ';
	var valid = 1;
	var empty_fields = '';
	var errors = '';

	//The 'for' loop below checks the value for each element of the form 
	//elements(by iteration) calling the appropriate functions  
   
	for (var i = 0; i < f.length; i++) {
		var e = f.elements[i];
       	if ( ( (e.type == 'text') || (e.type == 'textarea') )  && (e.name != "form_field_mobile") ) {
			if ((e.value == null) || (e.value == '') || isblank(e.value)) {
       			empty_fields += '\n- ' + field_hash(e.name);
				continue;
       		}
		}
	}
	
	if ( f.form_field_name != null ) {
		if ( (f.form_field_name.value != '') && !isblank(f.form_field_name.value) )
        	if (CheckName(f.form_field_name.value)  == 0) 
	        	errors += '\n- ' + field_hash(f.form_field_name.name) + ' is not a valid name.' ;
	}

	if (f.form_field_town != null) {
		if ((f.form_field_town.value != null) &&  (f.form_field_town.value != '') && !isblank(f.form_field_town.value))
        	if (CheckName(f.form_field_town.value)  == 0) 
	        	errors += '\n- ' + field_hash(f.form_field_town.name) + ' is not a valid name of a town.' ;
	}
	
	if (f.form_field_country != null) {
		if ((f.form_field_country.value != null) &&  (f.form_field_country.value != '') && !isblank(f.form_field_country.value))
        	if (CheckName(f.form_field_country.value)  == 0) 
	        	errors += '\n- ' + field_hash(f.form_field_country.name) + ' is not a valid name of a country.' ;
	}
	
	if (f.form_field_email != null) {
		if ((f.form_field_email.value != null) && (f.form_field_email.value != '')  && !isblank(f.form_field_email.value) && (navigator.appVersion.substring(0,1)!='3'))
			if (CheckEmail(f.form_field_email.value)==0)
				errors+='\n- ' + field_hash(f.form_field_email.name) + ' does not seem like a valid email address.' ;
	}
				
	if (f.form_field_mobile != null) {
		if ((f.form_field_mobile.value!='') && !isblank(f.form_field_mobile.value))
			if (CheckPhoneNumber(f.form_field_mobile.value) == 0)
				errors += '\n- ' + field_hash(f.form_field_mobile.name) + ' is not a valid mobile number.' ;
	}

	// output and feedback
	if (!empty_fields && !errors)  return true;
	
	if (empty_fields) msg += '\n- The following required field(s) are empty :' + empty_fields + '\n';
	alert(msg+errors);
	return false;
		
} // end of verify()
