//////////////////////////////////////////////////////////
//USER
//////////////////////////////////////////////////////////
function userLogin(){
    var message = "";
    if (getTextVal('email') == "") message += "\nMissing Email / Username";
    if (getTextVal('password') == "") message += "\nMissing Password";

    if (message != ""){
	    alert("Please fix the following:"+message);
	    return false;
    } else {
        return true;
    }
}

function isUserTaken(field){
    var val = getTextVal(field);

    if (field == "email2"){
        if (val != ""){
            if (isValidEmail(val)){
                javascript:xmlHTTP('Public::User','isEmailTaken',param('email='+val),'','','0');
            } else {
                getItem('email2').value="";
                getItem('div_EmailIsTaken').innerHTML="<img src='/static/images/publlic/no.gif' border='0' title='"+val+" is not a valid email.'>";
            }
        } else {
            getItem('div_EmailIsTaken').innerHTML = "";
        }

    } else if (field == "username2") {
        if (val != ""){
            javascript:xmlHTTP('Public::User','isUsernameTaken',param('username='+val),'','','0');
        } else {
            getItem('div_UsernameIsTaken').innerHTML = "";
        }
    }

}



function userRegister(){
    var message = "";
    if (getTextVal('email2') == "") message += "\nMissing Email";
    else if (!isValidEmail(getTextVal('email2'))) message += "\nInvalid Email";
    if (getTextVal('username2') == "") message += "\nMissing Username";
    if (getTextVal('password2') == "") message += "\nMissing Password";
    if (getTextVal('password_confirm2') == "") message += "\nMissing Confirm Password";
    else if (getTextVal('password2') != getTextVal('password_confirm2')) message += "\nConfrim Password does not match";

    if (message != ""){
	    alert("Please fix the following:"+message);
	    return false;
    } else {
        return true;
    }
}

function userForgotPassword(){
    var message = "";
    if (getTextVal('email3') == "") message += "\nMissing Email";
    else if (!isValidEmail(getTextVal('email3'))) message += "\nInvalid Email";

    if (message != ""){
	    alert("Please fix the following:"+message);
	    return false;
    } else {
        return true;
    }
}
function userPasswordReset(){
    var message = "";
    if (getTextVal('password') == "") message += "\nMissing Password";
    if (getTextVal('password_confirm') == "") message += "\nMissing Confirm Password";
    else if (getTextVal('password') != getTextVal('password_confirm')) message += "\nConfrim Password does not match";

    if (message != ""){
	    alert("Please fix the following:"+message);
	    return false;
    } else {
        return true;
    }
}
//////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
//BUSINESS
//////////////////////////////////////////////////////////
function isBusinessEmailTaken(field){
    var val = getTextVal(field);
    javascript:xmlHTTP('Public::Business','isEmailTaken',param('email='+val),'','','0');
}

function saveBusiness(){
    var message = "";
    if (getTextVal('Name') == "") message += "\nMissing Busines Name";
    if (getTextVal('Contact') == "") message += "\nMissing Contact Person";
    if (getTextVal('Phone') == "") message += "\nMissing Phone";
    //if (getTextVal('Cell') == "") message += "\nMissing Cell";
    if (getTextVal('Email') == "") message += "\nMissing Email";
    else if (!isValidEmail(getTextVal('Email'))) message += "\nInvalid Email";
    //if (getTextVal('Website') == "") message += "\nMissing Busines Name";
    if (message != ""){
	alert("Please fix the following:"+message);
	return false;
    } else {
	return true;
    }
}

function setAddMoreBranch(AddMoreBranch){
    getItem('AddMoreBranch').value=AddMoreBranch;
}
function saveBranch(AddMoreBranch){
    var message = "";
    if (getTextVal('Contact') == "") message += "\nMissing Contact Person";
    if (getTextVal('Phone') == "") message += "\nMissing Phone";
    //if (getTextVal('Cell') == "") message += "\nMissing Cell";
    if (getTextVal('Email') == "") message += "\nMissing Email";
    else if (!isValidEmail(getTextVal('Email'))) message += "\nInvalid Email";
    //if (getTextVal('Website') == "") message += "\nMissing Busines Name";
    if (getTextVal('Address1') == "") message += "\nMissing Address 1";
    if (getSelectVal('City') == "") message += "\nMissing City";
    if (getSelectVal('Province') == "") message += "\nMissing Province";
    if (getTextVal('Postal') == "") message += "\nMissing Postal Code";
    if (getTextVal('CategoryID') == "") message += "\nMissing Category";

    if (message != ""){
	alert("Please fix the following:"+message);
	return false;
    } else {
	return true;
    }
}

function showCategories(){
    var itm = getItem('tableAllCategories');
    itm.style.display="";
}

function selectCategory(id, val){
    getItem('CategoryID').value=id;
    getItem('Category').value=val;
    getItem('tableAllCategories').style.display="none";
}

function Save(counter){
    alert("here");
    var message = "";
    if (counter == "0"){
	if (getTextVal('Name') == "") message += "\nMissing Busines Name";
	if (message != ""){
	    alert("Please fix the following:"+message);
	} else {
	    document.myform.submit();
	}
    } else {
	if (getTextVal('BusinessID') == "") message += "\n Business Name";
	if (getTextVal('Contact_'+counter) == "") message += "\n Branch # "+ counter + " Contact Person";
	if (getTextVal('Phone_'+counter) == "") message += "\n Branch # "+ counter + " Phone";
	if (getTextVal('Address1_'+counter) == "") message += "\n Branch # "+ counter + " Address 1";
	if (getSelectVal('City_'+counter) == "") message += "\n Branch # "+ counter + " City";
	if (getSelectVal('Province_'+counter) == "") message += "\n Branch # "+ counter + " Province";
	if (getTextVal('Postal_'+counter) == "") message += "\n Branch # "+ counter + " Postal Code";
	if (getSelectVal('Category_'+counter) == "") message += "\n Branch # "+ counter + " Sub Category";

	if (message != ""){
	    alert("Please fix the following:"+message);
	} else {
	    if (getTextVal('BranchID_'+counter) == ""){
		javascript:xmlHTTP('Business::Register','processBranch',param('todo=add,counter='+counter+',BusinessID|Text,BranchID_'+counter+'|Text,Contact_'+counter+'|Text,Phone_'+counter+'|Text,Fax_'+counter+'|Text,Email_'+counter+'|Text,Website_'+counter+'|Text,Address1_'+counter+'|Text,Address2_'+counter+'|Text,Province_'+counter+'|Select,City_'+counter+'|Select,Postal_'+counter+'|Text,ParentCategory_'+counter+'|Text,Category_'+counter+'|Text'),'','div_'+counter,'0');
		counter++;
		javascript:xmlHTTP('Business::Register','new_branch',param('counter='+counter),'','branches','1');
	    } else {
		javascript:xmlHTTP('Business::Register','processBranch',param('todo=edit,counter='+counter+',BusinessID|Text,BranchID_'+counter+'|Text,Contact_'+counter+'|Text,Phone_'+counter+'|Text,Fax_'+counter+'|Text,Email_'+counter+'|Text,Website_'+counter+'|Text,Address1_'+counter+'|Text,Address2_'+counter+'|Text,Province_'+counter+'|Select,City_'+counter+'|Select,Postal_'+counter+'|Text,ParentCategory_'+counter+'|Text,Category_'+counter+'|Text'),'','div_'+counter,'0');
	    }
	}
    }
}


function getProvinces(){
    var country = getSelectVal('Country');
    javascript:xmlHTTP('Public::Business','getProvinces',param('country='+country),'','','0');
}

function getCities(){
    var country = getSelectVal('Country');
    var province = getSelectVal('Province');
    javascript:xmlHTTP('Public::Business','getCities',param('country='+country+'&province='+province),'','div_city','0');
}


function showCategory(id){

    var tags = document.getElementsByTagName('table');
    for(var i = 0; i < tags.length; i++){
        var thisTag = tags[i].id;
        if (thisTag.substring(0,14) == "ParentCategory"){
            getItem(thisTag).style.display='none';
        }
    }
    getItem('ParentCategory'+id).style.display='';

}

///////////////////////////////////////////////////////////////////////////////////
//SRW

function setStar(id, num){
    if (num == "") num=0;
		icon = "star";
    if (id == "price") icon="dollar";
		for (var x=1; x<=num; x++){
        getItem(id+"_"+x).src="/static/images/publlic/full"+icon+".png";
    }
    for (var x=(parseInt(num)+parseInt(1)); x<=5; x++){
        getItem(id+"_"+x).src="/static/images/publlic/no"+icon+".png";
    }
    getItem(id+"_rating").value = num;
}


function rateBranch(){
    var branchid = getTextVal('branchid');
    var quality = getTextVal('quality_rating');
    var service = getTextVal('service_rating');
    var price = getTextVal('price_rating');
    var cleanliness = getTextVal('cleanliness_rating');
    var environment = getTextVal('environment_rating');
    var comment = getTextVal('comment');
    javascript:xmlHTTP('Public::SRW','rateBranch',param('branchid='+branchid+'&quality='+quality+'&service='+service+'&price='+price+'&cleanliness='+cleanliness+'&environment='+environment+'&comment='+comment),'','','0');
}
function addToWatchList(){
    var branchid = getTextVal('branchid');
    javascript:xmlHTTP('Public::SRW','addToWatchList',param('branchid='+branchid),'','','0');
}
function reportThisPage(branchid){
    alert(branchid);
    javascript:xmlHTTP('Public::SRW','reportThisPage',param('branchid='+branchid),'','','0');
}
// Compatible with all major browsers for getting an instance of an object
function getItem(id){
    var itm = false;
    if(document.getElementById)itm = document.getElementById(id);
    else if(document.all)itm = document.all[id];
    else if(document.layers)itm = document.layers[id];
    return itm;
}

// Extract the value from a Text input box
function getTextVal (id){
    return getItem(id).value;
}

// Exrtact the value from a select list
function getSelectVal (id){
    var item = getItem(id);
    return item.options[item.selectedIndex].value;
}

// Extract a value from radio buttons
function getRadioVal (id){
    var selection = getItem(id);
    for (i=0; i<selection.length; i++)
	if (selection[i].checked == true)
	return selection[i].value;
}

function isValidEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true
	}

